package cn.imvc.app.controllers.goods;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import cn.imvc.app.entities.AttrClass;
import cn.imvc.app.entities.AttrValue;
import cn.imvc.app.models.AttrClassModel;
import cn.imvc.app.models.AttrValueModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年7月23日上午11:16:35
 *
 */
public class AttrValueControllerImpl implements AttrValueController{
    
	public Page<AttrValue> page;
	public ViewRequest viewRequest;
	public AttrValue attrValue;
	public int id;
	public int[] ids;
	private String search_content;
	public int attrClassId;
	public AttrClass attrclass;
	static Logger logger = Logger.getLogger(AttrValueControllerImpl.class);
	@Autowired
	private AttrValueModel attrValueModel;
	
	@Autowired
	private AttrClassModel attrClassModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return null;
	}

	public String index() {
		
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		
		viewRequest.getSort().put("sort", "ASC");
		
		if(search_content!=null&&!search_content.equals("")){
		
			viewRequest.getParams().put("name_like", search_content);
			
		}
		
		if(attrClassId != 0){
			viewRequest.getParams().put("_and_attrClass_eq_number",""+attrClassId );
		}
		page = attrValueModel.find(viewRequest);
		return "attr-value/index";
		
	}
	
	
	public String show() {
		
		attrValue = attrValueModel.get(id);
		return "attr-value/edit";
		
	}

	public String editNew() {
		attrclass = attrClassModel.get(attrClassId);
		return "attr-value/editNew";
	}

	public String create() {
		
		attrValue.setAttrClass(attrClassModel.get(attrClassId));
		int result = attrValueModel.save(attrClassId,attrValue);
			if(result ==-1){
			 return Render.json(new DialogBean("300","属性值名称不能重复","attr-value","closeCurrent"));
			}
			loggingModel.info(logger,"添加属性值："+attrValue.getName());
		return Render.json(new DialogBean("200","添加成功","attr-value","closeCurrent"));
	}

	public String edit() {
		
		attrValue = attrValueModel.get(id);
		return "attr-value/edit";
	}

	public String update() {
		
		attrclass = attrClassModel.get(attrClassId);
		int result = attrValueModel.update(id,attrclass,attrValue);
			if(result ==-1){
			 return Render.json(new DialogBean("300","属性值名称不能重复","attr-value","closeCurrent"));
			}
			loggingModel.info(logger,"修改属性值："+attrValue.getName());
		return Render.json(new DialogBean("200","修改成功","attr-value","closeCurrent"));
	}

	public String destroy() {
		String result = attrValueModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除属性值："+id);
		    return Render.json(new DialogBean("200","删除成功","attr-value"));
		}else{
			return Render.json(new DialogBean("300",result,"attr-value"));
		}
	}

	public String destroyBatch() {
		attrValueModel.delete(ids);
		return Render.json(new DialogBean("200","删除成功","attr-value"));
	}
	/**
	 * 外部调用
	 * @return
	 */
public String dialog() {
		
	if(viewRequest==null){
		viewRequest = new ViewRequest();
	}
	viewRequest.getSort().put("sort", "ASC");
	
	if(search_content!=null&&!search_content.equals("")){
	
		viewRequest.getParams().put("name_like", search_content);
		
	}
	if(attrClassId != 0){
		viewRequest.getParams().put("_and_attrClass_eq_number",""+attrClassId );
	}
	page = attrValueModel.find(viewRequest);
		return "attr-value/dialog";
	}
	
	
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public AttrValue getAttrValue() {
		return attrValue;
	}

	public void setAttrValue(AttrValue attrValue) {
		this.attrValue = attrValue;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}
	
	public AttrClass getAttrclass() {
		return attrclass;
	}

	public void setAttrclass(AttrClass attrclass) {
		this.attrclass = attrclass;
	}


	public int getAttrClassId() {
		return attrClassId;
	}

	public void setAttrClassId(int attrClassId) {
		this.attrClassId = attrClassId;
	}


}
